﻿/// Write a program that finds the maximal increasing sequence in an array. 
/// Example: {3, 2, 3, 2, 2, 4}  {2, 3, 4}.

using System;
using System.Collections;

class zad_5
{
    static void Main()
    {
        int[] arr = { 2, 3, 4, 5, 6, 3, 4, 2, 2, 4, 2, 3, 4, 5, 6, 7 };

        ArrayList bestSequence = new ArrayList(); // keep best sequence
        ArrayList tempSequence = new ArrayList(); // temporary best sequence

        // init counters
        int sequenceLenght = 1;
        int sequenceBestLenght = 1;

        for (int i = 0; i < arr.GetLength(0) - 1; i++)
        {

            if (arr[i+1] == arr[i] + 1)
            {
                sequenceLenght++;
                // add first element only !
                if (sequenceLenght == 2)
                {
                    tempSequence.Add(arr[i]);
                }
                // aways add next element 
                tempSequence.Add(arr[i+1]);

                // if better sequence found copy it
                if (sequenceLenght >= sequenceBestLenght)
                {
                    sequenceBestLenght = sequenceLenght;
                    bestSequence = new ArrayList(tempSequence);
                }

            }
            else
            {
                // reset tempotary counters
                sequenceLenght = 1;
                tempSequence.Clear();
            } 
        }

        Console.WriteLine("maximal increasing sequence contains {0} elements:", sequenceBestLenght);
        
        foreach (var item in bestSequence)
        {
            Console.Write(item + " " );
        }
        
        Console.WriteLine();
    }
}
